package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleConfigService;
import com.heima.article.service.ApArticleContentService;
import com.heima.article.service.ApArticleService;
import com.heima.common.constants.ArticleConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.article.dtos.ArticleCommentDto;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.dtos.DelCommentDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.article.vos.ArticleCommnetVo;
import com.heima.model.article.vos.HotArticleVo;
import com.heima.model.comment.dtos.CommentConfigDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author itheima
 */
@Slf4j
@Service

public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {



    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private ApArticleContentService apArticleContentService;

    @Autowired
    private ApArticleConfigService apArticleConfigService;


    @Autowired
    private CacheService cacheServicel;


    @Autowired
    MInIOService mInIOService;
    @Override
    public List<ApArticle> loadAll(ArticleHomeDto articleHomeDto,Integer status) {

        return apArticleMapper.loadAll(articleHomeDto,status);
    }

    @Override
    @Transactional
    public Long saveDto(ArticleDto dto) {

        //保存app_article
        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto, apArticle);
        apArticle.setId(null);
        this.save(apArticle);

        //保存app_article_content
        ApArticleContent apArticleContent = new ApArticleContent();
        apArticleContent.setArticleId(apArticle.getId());
        apArticleContent.setContent(dto.getContent());
        apArticleContentService.save(apArticleContent);

        //保存app_article_config
        ApArticleConfig apArticleConfig = new ApArticleConfig(apArticle.getId());
        apArticleConfigService.save(apArticleConfig);

        //异步调用生成静态文件
        mInIOService.sendMInIO(apArticleContent);

        return apArticle.getId();
    }

    @Override
    public PageResponseResult findCommentByPage(ArticleCommentDto dto) {
        List<ArticleCommnetVo> articleCommnetVoList=apArticleMapper.findCommentByPage(dto);
        LambdaQueryWrapper<ApArticle> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(ApArticle::getAuthorId,dto.getWmUserId());
        Integer integer = apArticleMapper.selectCount(lambdaQueryWrapper);
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage().longValue(),dto.getSize().longValue(),integer);
        pageResponseResult.setData(articleCommnetVoList);
        return pageResponseResult;
    }

    @Override
    public ResponseResult delComment(DelCommentDto delCommentDto) {
        Long id = delCommentDto.getArticleId();
        ApArticle byId = this.getById(id);
        byId.setLikes(byId.getLikes()-delCommentDto.getDelLikes());
        byId.setComment(byId.getComment()-delCommentDto.getDelComments());

        this.updateById(byId);

        return ResponseResult.okResult("减少成功");
    }

    @Override
    public ResponseResult changeArticleStatus(CommentConfigDto dto) {

        LambdaQueryWrapper<ApArticleConfig> apArticleConfigLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apArticleConfigLambdaQueryWrapper.eq(ApArticleConfig::getArticleId,dto.getArticleId());
        ApArticleConfig oneInDb = apArticleConfigService.getOne(apArticleConfigLambdaQueryWrapper);
        if (dto.getOperation() == 1){
            oneInDb.setIsComment(true);
        }else {
            oneInDb.setIsComment(false);
            ApArticle byId = this.getById(dto.getArticleId());
            byId.setComment(0);
            this.updateById(byId);
        }
        apArticleConfigService.updateById(oneInDb);


        return ResponseResult.okResult("操作成功");
    }

    @Override
    public List<HotArticleVo> loadAll2(ArticleHomeDto articleHomeDto, int statu) {
        String s = cacheServicel.get(ArticleConstants.HOT_ARTICLE_FIRST_PAGE + articleHomeDto.getTag());
        List<HotArticleVo> hotArticleVos = JSON.parseArray(s, HotArticleVo.class);


        return hotArticleVos;
    }


}

